﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dukou.Javascript
{
    public class JsLogicalExpr : JsExpr
    {
        public object Left { get; private set; }

        public object Right { get; private set; }

        public JsLogicalOperator LogicalOperator { get; set; }

        public JsLogicalExpr(object left)
            : this(left, null, JsLogicalOperator.Not)
        {
 
        }

        public JsLogicalExpr(object left, object right, JsLogicalOperator opt)
        {
            this.Left = left;
            this.Right = right;
            this.LogicalOperator = opt;
        }

        public override string ToString()
        {
            string left = this.Left.ToString();
            if (this.Left as JsVar == null && this.Left as JsExpr == null)
            {
                left = JsValue.Create(this.Left).ToString();
            }
            if (this.LogicalOperator == JsLogicalOperator.Not)
            {
                return string.Format("{0}({1})", GetOperator(this.LogicalOperator), left);
            }

            string right = this.Right.ToString();
            if (this.Right as JsVar == null && this.Right as JsExpr == null)
            {
                right = JsValue.Create(this.Right).ToString();
            }

            return string.Format("({0}) {1} ({2})", left, GetOperator(this.LogicalOperator), right);
        }

        public static string GetOperator(JsLogicalOperator opt)
        {
            switch (opt)
            {
                case JsLogicalOperator.Not:
                    return "!";
                case JsLogicalOperator.And:
                    return "&&";
                case JsLogicalOperator.Or:
                    return "||";
                default:
                    return "";
            }
        }
    }
}
